package com.xinou.daily_report.controller;


import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.rule.RuleListPO;
import com.xinou.daily_report.PO.rule.RuleUpdatePO;
import com.xinou.daily_report.VO.RulesInfoVO;
import com.xinou.daily_report.service.SysRulesService;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-07-08
 */
@RestController
@RequestMapping("/admin/rules")
@Api(tags = {"后台 规则"})
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRulesController {

    private final FileUploadService fileUploadService;
    private final SysRulesService sysRulesService;

    @PostMapping("list")
    @ApiOperation("获取规则列表")
    @RequiresPermissions({"admin:rules:list"})
    public ApiResponse<RulesInfoVO> getList(@RequestBody RuleListPO po){
        return sysRulesService.ruleList(po);
    }

    @PostMapping("update")
    @ApiOperation("修改规则")
    @RequiresPermissions({"admin:rules:update"})
    public ApiResponse<Void> ruleUpdate(@RequestBody RuleUpdatePO po){
        return sysRulesService.ruleUpdate(po);
    }

    @PostMapping("file/upload")
    @ApiOperation("富文本文件上传")
    @RequiresPermissions({"admin:rules:upload"})
    public ApiResponse<String> fileUpload(MultipartFile file){
        String s = fileUploadService.fileUpload(file).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "文件上传失败"));
        Map<String,String>map=new HashMap<>(1);
        map.put("fileName",s);
        return new ApiResponse<>(map);
    }
}

